local ItemDefine = require("bag.ItemDefine")
--local Util = require("common.Util")
local EquipExcel = require("excel.Equip")
local EquipConf = EquipExcel.equip
local ObjHuman = require("core.ObjHuman")
local BagLogic = require("bag.BagLogic")
local Lang = require("common.Lang")
local HtmlUtil = require("common.HtmlUtil")
local EquipLogic = require("bag.EquipLogic")
local Status = require("scene.Status")
local Grid =require("bag.Grid")


local os = _G.os
local string = _G.string

function makeGridsByIndexs(grids, msgRet, indexs, human)
    msgRet.itemDatas[0] = #indexs
    for k, v in ipairs(indexs) do
        if not grids[v] then
            assert()
        end
        Grid.makeItem(grids[v], msgRet.itemDatas[k], k, human)
    end
end

function makeGrid(grid, itemData, index, human)
    Grid.makeItem(grid, itemData, index, human)
end

function swap(bag, cap, srcIndex, tarIndex)
    if not checkIndex(bag, srcIndex, cap) or not checkIndex(bag, tarIndex, cap) then
        return false
    end
    if bag[srcIndex].id == 0 then
        return false
    end

    if bag[tarIndex].id == 0 then
        Grid.copy(bag[tarIndex], bag[srcIndex])
        bag[srcIndex].id = 0
    else

        if bag[tarIndex].id ~= bag[srcIndex].id or bag[tarIndex].bind ~= bag[srcIndex].bind or bag[tarIndex].lifeTime ~= bag[srcIndex].lifeTime or
            not ItemDefine.canAppend(bag[srcIndex].id) or bag[tarIndex].cnt == ItemDefine.CAP_PER_GRID then
            local tmp = {}
            Grid.copy(tmp, bag[srcIndex], true)
            Grid.copy(bag[srcIndex], bag[tarIndex], true)
            Grid.copy(bag[tarIndex], tmp)
        else
            --可合并
            local cntReal = math.min(bag[srcIndex].cnt, ItemDefine.CAP_PER_GRID - bag[tarIndex].cnt)
            bag[tarIndex].cnt = bag[tarIndex].cnt + cntReal
            bag[srcIndex].cnt = bag[srcIndex].cnt - cntReal
            if bag[srcIndex].cnt == 0 then
                bag[srcIndex].id = 0
            end
        end
    end
    return true
end

function swap2(srcBag, srcIndex, tarBag, tarIndex)
    local tmp = {}
    if not srcBag[srcIndex] or not tarBag[tarIndex] then
        Grid.copy(tmp, srcBag[srcIndex], true)
        Grid.copy(srcBag[srcIndex], tarBag[tarIndex], true)
        Grid.copy(tarBag[tarIndex], tmp)
    else
        --local cnt = transfer(srcBag[srcIndex], tarBag[tarIndex], capPerGrid)
        if tarBag[tarIndex].id ~= srcBag[srcIndex].id or tarBag[tarIndex].bind ~= srcBag[srcIndex].bind or tarBag[tarIndex].lifeTime ~= srcBag[srcIndex].lifeTime or
            not ItemDefine.canAppend(srcBag[srcIndex].id) or tarBag[tarIndex].cnt == ItemDefine.CAP_PER_GRID then
            Grid.copy(tmp, srcBag[srcIndex], true)
            Grid.copy(srcBag[srcIndex], tarBag[tarIndex], true)
            Grid.copy(tarBag[tarIndex], tmp)
        else
            local cntReal = math.min(srcBag[srcIndex].cnt, ItemDefine.CAP_PER_GRID - tarBag[tarIndex].cnt)
            tarBag[tarIndex].cnt = tarBag[tarIndex].cnt + cntReal
            srcBag[srcIndex].cnt = srcBag[srcIndex].cnt - cntReal
        end
        if srcBag[srcIndex].cnt == 0 then
            srcBag[srcIndex].id = 0
        end
        if tarBag[tarIndex].cnt == 0 then
            tarBag[tarIndex].id = 0
        end
    end
    return true
end

--function swap2(srcBag, srcIndex, tarBag, tarIndex)
--	if not srcBag[srcIndex] or not tarBag[tarIndex] then
--		srcBag[srcIndex], tarBag[tarIndex] = tarBag[tarIndex], srcBag[srcIndex]
--	else
--		local cnt = transfer(srcBag[srcIndex], tarBag[tarIndex], capPerGrid)
--		if cnt < 1 then
--			srcBag[srcIndex], tarBag[tarIndex] = tarBag[tarIndex], srcBag[srcIndex]
--		end
--		if srcBag[srcIndex].cnt == 0 then
--			srcBag[srcIndex] = nil
--		end
--		if tarBag[tarIndex].cnt == 0 then
--			tarBag[tarIndex] = nil
--		end
--	end
--	return true
--end


function divide(bag, cap, srcIndex, tarIndex, cnt)
    if not checkIndex(bag, srcIndex, cap) then
        return false
    end

    if not checkIndex(bag, tarIndex, cap) then
        return false
    end

    if bag[srcIndex].id == 0 then
        return false
    end

    if bag[tarIndex].id ~= 0 then
        return false
    end

    if ItemDefine.isEquip(bag[srcIndex].id) then
        return false
    end

    if cnt < 1 or cnt > bag[srcIndex].cnt then
        return false
    end
    Grid.copy(bag[tarIndex], bag[srcIndex])
    bag[tarIndex].uuid = _G.gen_oid()
    bag[tarIndex].cnt = cnt
    bag[srcIndex].cnt = bag[srcIndex].cnt - cnt
    if bag[srcIndex].cnt == 0 then
        bag[srcIndex].id = 0
    end
    return true
end


--[[
function cmp(a, b)
if a.id ~= b.id then return a.id < b.id end
if a.bind ~= b.bind then return a.bind end
if a.cnt ~= b.cnt then return a.cnt > b.cnt end
if a.lifeTime ~= b.lifeTime then
if not a.lifeTime or not b.lifeTime then
return a.lifeTime
end
return a.lifeTime < b.lifeTime
end

if a.equip and b.equip then
if a.equip.quality ~= b.equip.quality then return a.equip.quality < b.equip.quality end
end
end
--]]

function checkIndex(bag, index, cap)
    return index and 0 <= index and index < cap
end

function checkGridOverTime()

    local now = os.time()
    for _, human in pairs(ObjHuman.onlineUuid) do

        --bag
        for index, grid in pairs(human.db.bag) do
            if index ~= 0 and grid.lifeTime and now > grid.lifeTime then
                BagLogic.delGridByIndex(human, index, "grid_over_time")
                Broadcast.sendBroadcast(human, Lang.CHAT, string.format(Lang.BAG_GRID_OVER_TIME, HtmlUtil.fontItem(grid)))
            end
        end

        --equip
        local calc = nil
        for index, grid in pairs(human.db.equip) do
            if index ~= 0 and grid.lifeTime and now > grid.lifeTime then
                calc = true
                local conf = EquipConf[grid.id]
                human.db.equip[index] = nil
                EquipLogic.GC_EQUIP_CHANGE(human, index)
                if conf.pos == ItemDefine.WEAPON or conf.pos == ItemDefine.CLOTH
                    or conf.pos == ItemDefine.HORSE then
                    Status.sendViewStatus(human, true)
                    RoleHandler.CG_BODY_INFO(human)
                end
                Broadcast.sendBroadcast(human, Lang.CHAT, string.format(Lang.EQUIP_GRID_OVER_TIME, HtmlUtil.fontItem(grid)))
            end
        end

        if calc then
            ObjHuman.doCalc(human)
        end

        --store
        for index, grid in pairs(human.db.store) do
            if index ~= 0 and grid.lifeTime and now > grid.lifeTime then
                human.db.store[index] = nil
                StoreLogic.sendStoreItemChange(human, index)
                Broadcast.sendBroadcast(human, Lang.CHAT, string.format(Lang.STORE_GRID_OVER_TIME, HtmlUtil.fontItem(grid)))
            end
        end

    end
end
